package com.jstyle.jclifexd.activity;

import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.adapter.NotificationAdapter;
import com.jstyle.jclifexd.model.Created;
import com.jstyle.jclifexd.utils.RxBus;
import com.jstyle.jclifexd.utils.StatusBarUtil;
import com.jstyle.jclifexd.utils.Utils;
import java.util.HashMap;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 活动
 */
public class NotificationlistActivity extends BaseActivity {
    @BindView(R.id.activity_RecyclerView)
    RecyclerView activity_RecyclerView;
    NotificationAdapter activitvtiesAdapter;
    NotificationManager notificationManager;
    @SuppressLint("CheckResult")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notificationlist);
        StatusBarUtil.setRootViewFitsSystemWindows(this,true);
        StatusBarUtil.setTranslucentStatus(this);
        if (!StatusBarUtil.setStatusBarDarkTheme(this, true)) {
            StatusBarUtil.setStatusBarColor(this,0x55000000);
        }
        ButterKnife.bind(this);
        activity_RecyclerView.setLayoutManager(new LinearLayoutManager(this));
        activitvtiesAdapter = new NotificationAdapter(this);
        activity_RecyclerView.setAdapter(activitvtiesAdapter);
        Querydata();
        notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(0);
        /**
         * 如果退出了活动当前界面需要刷新状态
         */
        RxBus.getInstance().toObservable(String.class).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<String>() {
            @Override
            public void accept(String bleData) {
                if (null != bleData) {
                    if (bleData.equals("reload_NotificationlistActivity")) {
                        Querydata();
                    }
                }
            }
        });
    }

    private void Querydata() {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", NetWorkUtil.getUserId());//"uid" -> "111000036"
        NetWorkUtil.getInstance().getJstyleApi().queryAuditAll(map).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Created>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(Created activitvtiesData) {
                activitvtiesAdapter.SetData(activitvtiesData.getData());
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        });

    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        if (Utils.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;

        }
    }

}
